package com.smedi.ismedi.constructionproject.core.application.query.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smedi.ismedi.constructionproject.core.application.query.BidSectionQueryService;
import com.smedi.ismedi.constructionproject.core.application.query.vo.BidSectionVo;
import com.smedi.ismedi.constructionproject.core.domain.aggregatemodel.BidSection;
import com.smedi.ismedi.constructionproject.core.domain.aggregatemodel.BidSectionConfig;
import com.smedi.ismedi.constructionproject.core.domain.aggregatemodel.BidSectionConfigOrganization;
import com.smedi.ismedi.constructionproject.core.domain.repository.BidSectionConfigOrganizationRepository;
import com.smedi.ismedi.constructionproject.core.domain.repository.BidSectionConfigRepository;
import com.smedi.ismedi.constructionproject.core.domain.repository.BidSectionRepository;
import com.smedi.ismedi.constructionproject.core.domain.service.ProjectBasicDomainService;
import com.smedi.ismedi.constructionproject.core.infrastructure.constant.CustomError;
import com.smedi.ismedi.constructionproject.core.port.adapter.persistence.mybatis.mapper.BidSectionMapper;
import com.smedi.ismedi.constructionproject.core.port.adapter.service.OrganizationRemoteApiService;
import com.smedi.ismedi.organization.api.dto.response.ProjectOrganizationResponse;
import com.smedi.ismedi.web.starter.exception.LogicException;
import com.smedi.ismedi.web.starter.util.DozerUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: xulinjun
 * @Date: 2021-6-22 16:40
 */
@Slf4j
@Service
@AllArgsConstructor
public class BidSectionQueryServiceImpl implements BidSectionQueryService {
    private final ProjectBasicDomainService projectBasicDomainService;

    private final OrganizationRemoteApiService organizationRemoteApiService;

    private final BidSectionMapper bidSectionMapper;

    private final BidSectionRepository bidSectionRepository;

    private final BidSectionConfigRepository bidSectionConfigRepository;

    private final BidSectionConfigOrganizationRepository bidSectionConfigOrganizationRepository;

    private final DozerUtil dozerUtil;

    @Override
    public IPage getBidSectionPaginationByProjectId(Long offset, Long pageSize, Long projectId) {
        // 判断组织是否存在
        boolean validate = projectBasicDomainService.validateProject(projectId);
        if (!validate) {
            throw LogicException.raise(CustomError.PROJECT_NOT_AVAILABLE);
        }

        Page<BidSectionVo> page = new Page<>(offset, pageSize);
        page.addOrder(new OrderItem().ascs("id"));

        IPage<BidSectionVo> pagination = bidSectionMapper.selectBidSectionInfoVoPagination(page, projectId);
        pagination.getRecords().stream().forEach(bidSectionViewVo -> {
            // 业主单位
            if (bidSectionViewVo.getOwnerUnitId() != null) {
                ProjectOrganizationResponse ownerUnit = organizationRemoteApiService.getProjectOrganizationView(bidSectionViewVo.getOwnerUnitId());
                if (ownerUnit != null) {
                    bidSectionViewVo.setOwnerUnit(ownerUnit.getFullName());
                }
            }
            // 设计单位
            if (bidSectionViewVo.getDesignInstituteId() != null) {
                ProjectOrganizationResponse designInstitute = organizationRemoteApiService.getProjectOrganizationView(bidSectionViewVo.getDesignInstituteId());
                if (designInstitute != null) {
                    bidSectionViewVo.setDesignInstitute(designInstitute.getFullName());
                }
            }
            // 施工单位
            if (bidSectionViewVo.getConstructionUnitId() != null) {
                ProjectOrganizationResponse constructionUnit = organizationRemoteApiService.getProjectOrganizationView(bidSectionViewVo.getConstructionUnitId());
                if (constructionUnit != null) {
                    bidSectionViewVo.setConstructionUnit(constructionUnit.getFullName());
                }
            }
            // 监理单位
            if (bidSectionViewVo.getSupervisionUnitId() != null) {
                ProjectOrganizationResponse supervisionUnit = organizationRemoteApiService.getProjectOrganizationView(bidSectionViewVo.getSupervisionUnitId());
                if (supervisionUnit != null) {
                    bidSectionViewVo.setSupervisionUnit(supervisionUnit.getFullName());
                }
            }
        });

        return pagination;
    }

    @Override
    public BidSectionVo getBidSectionByBidSectionId(Long bidSectionId) {
        BidSection bidSection = bidSectionRepository.selectByBidSectionId(bidSectionId);
        if (bidSection != null) {
            List<BidSectionConfig> bidSectionConfigs = bidSectionConfigRepository.selectListByBidSectionIdOrderById(bidSectionId);
            bidSectionConfigs.stream().forEach(bidSectionConfig -> {
                List<BidSectionConfigOrganization> bidSectionConfigOrganizations = bidSectionConfigOrganizationRepository.selectByBidSectionConfigIdOrderById(bidSectionConfig.getBidSectionConfigId());
                bidSectionConfig.setBidSectionConfigOrganizations(bidSectionConfigOrganizations);
            });
            bidSection.setBidSectionConfigs(bidSectionConfigs);

            return dozerUtil.convertor(bidSection, BidSectionVo.class);
        } else {
            throw LogicException.raise(CustomError.BID_SECTION_NOT_EXIST);
        }
    }

    @Override
    public List<BidSection> getBidSectionListByProjectId(Long projectId) {
        // 判断组织是否存在
        boolean validate = projectBasicDomainService.validateProject(projectId);
        if (!validate) {
            throw LogicException.raise(CustomError.PROJECT_NOT_AVAILABLE);
        }

        return bidSectionRepository.selectByProjectId(projectId);
    }
}
